package uk.ac.cam.bt288.Pico;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DBAdapter {
	public static final String KEY_ROWID = "id";
	public static final String KEY_APPKEY = "app_key";
	public static final String KEY_USERNAME = "username";
	public static final String KEY_PUBKEY = "public_key";
	public static final String KEY_PRIVKEY = "private_key";
	private static final String TAG = "DBAdapter";
	
	private static final String DATABASE_NAME = "pico";
	private static final String DATABASE_TABLE = "storage";
	private static final int DATABASE_VERSION = 2;
	
	private static final String DATABASE_CREATE = 
		"create table storage (id integer primary key autoincrement, "
		+ "app_key text not null, username text not null, public_key text not null, private_key text not null);";
	
	private final Context context;
	private DatabaseHelper DBHelper;
	private SQLiteDatabase db;
	
	//extend SQLiteOpenHelper to provide more functionalities
	private static class DatabaseHelper extends SQLiteOpenHelper {
		DatabaseHelper(Context context) {
			super(context, DATABASE_NAME, null, DATABASE_VERSION);
		}
		
		@Override
		public void onCreate (SQLiteDatabase db) {
			try {
				db.execSQL(DATABASE_CREATE);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		
		@Override
		public void onUpgrade (SQLiteDatabase db, int oldVersion, int newVersion) {
			Log.w(TAG, "Upgrading database from version "+oldVersion+" to "+newVersion+", which will destroy all old data");
			db.execSQL("DROP TABLE IF EXISTS storage");
			onCreate(db);
		}
		
	}
	
	public DBAdapter (Context ctx) {
		this.context = ctx;
		DBHelper = new DatabaseHelper(context);
	}
	
	//load database
	public DBAdapter load() throws SQLException {
		db = DBHelper.getWritableDatabase();
		return this;
	}
	
	//close database
	public void close(){
		DBHelper.close();
	}
	
	//insert new entry into database
	public long insertEntry(String app_key, String username, String pubKey, String privKey){
		ContentValues val = new ContentValues();
		val.put(KEY_APPKEY, app_key);
		val.put(KEY_USERNAME, username);
		val.put(KEY_PUBKEY, pubKey);
		val.put(KEY_PRIVKEY, privKey);
		return db.insert(DATABASE_TABLE, null, val);
	}
	
	//delete entry from database
	public boolean deleteEntry (long rowId){
		return db.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) > 0;
	}
	
	//delete all
	public boolean deleteAll () {
		return db.delete(DATABASE_TABLE, null, null) > 0;
	}
	
	//retrieve all entries
	public Cursor getAll(){
		return db.query(DATABASE_TABLE, new String[] {KEY_ROWID, KEY_APPKEY, KEY_USERNAME, KEY_PUBKEY, KEY_PRIVKEY}, null, null, null, null, null);
	}
	
	//retrieve a particular entry
	public Cursor getEntry (long rowId) throws SQLException {
		Cursor rtn = db.query(true, DATABASE_TABLE, new String[] {KEY_ROWID, KEY_APPKEY, KEY_USERNAME, KEY_PUBKEY, KEY_PRIVKEY}, KEY_ROWID + "=" + rowId, null, null, null, null, null);
		if (rtn != null)
			rtn.moveToFirst();
		return rtn;
	}
	
	//update an entry
	public boolean updateEntry(long rowId, String app_key, String username, String pubKey, String privKey) {
		ContentValues val = new ContentValues();
		val.put(KEY_APPKEY, app_key);
		val.put(KEY_USERNAME, username);
		val.put(KEY_PUBKEY, pubKey);
		val.put(KEY_PRIVKEY, privKey);
		return db.update(DATABASE_TABLE, val, KEY_ROWID + "=" + rowId, null) > 0;
	}
}
